


const typeObjectList = {
  jpg: 'image/jpg',
  png: 'image/png',
  jpeg: 'image/jpeg',
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  xls: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  ppt: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  pdf: 'application/pdf',
  doc: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  txt: 'text/plain',
  rar: "",
  zip: 'application/x-zip-compressed',
  ofd: 'application/ofd',
}
/**
 * 文件类型分组
 */
const fileTypeList = {
  "图片": ['jpg', 'png', 'jpeg'],
  "文档": ['xlsx', 'xls', 'pptx', 'ppt', 'pdf', 'doc', 'docx', 'txt'],
  "压缩包": ['zip', 'rar'],
  "发票": ['pdf', 'jpg', 'png', 'jpeg', 'doc', 'docx', 'zip', 'rar'],
  "PDF": ['pdf'],
  "发票1": ['pdf', 'jpg', 'png', 'jpeg', 'ofd'],
  "委托代征协议": ['pdf','jpg', 'png', 'jpeg'],
  "凭证": ['pdf','ofd','jpg', 'png', 'jpeg'],
  "": 'all'
}

/**
 * 文件分组的错误提示
 */
const fileTypeErrList = {
  "图片": '只允许上传jpg,jpeg,png格式的图片',
  "文档": "只允许上传 PDF,DOC,DOCX,XLS,XLSX 类型文件",
  "压缩包": "只允许上传 RAR 类型文件",
  "发票": '只允许上传 PDF,JPG, PNG,JPEG,DOC,DOCX,ZIP,RAR 类型文件',
  "PDF": '只允许上传 PDF 类型文件',
  "委托代征协议": '只允许上传 PDF,JPG, PNG,JPEG 类型文件',
  "凭证": '只允许上传 PDF,JPG,PNG,JPEG,OFD 类型文件',
  "发票1": '只允许上传 PDF,JPG, PNG,JPEG,OFD 类型文件',
}

/**
 * 文件类型图标
 */
const fileIcon = {
  pdf: 'icon-pdf',
  ppt: 'icon-ppt',
  pptx: 'icon-ppt',
  doc: 'icon-word',
  docx: 'icon-word',
  xlsx: 'icon-excel',
  xls: 'icon-excel',
  zip: 'icon-zip',
  rar: 'icon-zip',
  txt: 'icon-txt',
  jpg: 'icon-tupian',
  png: 'icon-tupian',
  jpeg: 'icon-tupian',
  'wz': 'icon-weizhigeshi',
}

/**
 * 获取文件后缀
 * @returns
 */
const getFileSuffix = (filePath) => {
  var startIndex = filePath.lastIndexOf(".");
  if (startIndex != -1)
    return filePath.substring(startIndex + 1, filePath.length).toLowerCase();
  else return "";
}

/**
 * 根据文件后缀获取对应的图标
 * @param {*} filePath
 * @returns
 */
const getFielIcon = (filePath, type,suffix) => {
  // console.log('getFielIcon===>',filePath, type,suffix);
  const fileSuffix = suffix|| getFileSuffix(filePath)
  const notData = ['jpg', 'png', 'jpeg']
  let res = fileIcon[fileSuffix]
  if (!res) {
    res = fileIcon['wz']
  }
  if (type === '1' && notData.indexOf(fileSuffix) >= 0) {
    res = 'icon-none'
  }

  return res
}

/**
 * 根据分组名称获取对应的文件后缀
 * @param {*} groupName
 * @returns
 */
const getFileGroupName = (fileSuffix, groupName, cardName) => {
  if (Object.prototype.toString.call(groupName) === '[object Object]') {
    const tempgroupName = groupName[cardName]
    if (tempgroupName === '') {
      return true
    }
    if (fileTypeList[tempgroupName].indexOf(fileSuffix) >= 0) {
      return true
    }
  }
  if (Object.prototype.toString.call(groupName) === '[object String]') {
    if (groupName === '') {
      return true
    }
    if (fileTypeList[groupName].indexOf(fileSuffix) >= 0) {
      return true
    }
  }
  return false
}

/**
 * 校验文件类型
 * @param {*} file
 * @param {*} typeNmae
 * @returns
 */
const checkFileType = (file, typeNmae, cardName) => {
  const { name, type } = file
  const res = {
    checkResult: false,
    mesg: ''
  }
  if (Object.prototype.toString.call(typeNmae) === '[object Object]') {
    // typeNmae 为对象时，并且为每一个cardName指定文件类型时就分别校验
    const temptypeNmae = typeNmae[cardName]
    const tempTypes = fileTypeList[temptypeNmae]
    const tempFileSuffix = getFileSuffix(name)
    if (temptypeNmae === '') {
      res.checkResult = true
      res.mesg = ''
    } else {
      if (tempTypes.indexOf(tempFileSuffix) >= 0) {
        res.checkResult = true
        res.mesg = ''
      } else {
        res.mesg = fileTypeErrList[temptypeNmae]
        res.checkResult = false
      }
    }
  }
  if (Object.prototype.toString.call(typeNmae) === '[object String]') {
    // typeNmae 为字符串时，所有的文件都执行同一个文件类型
    const tempTypes = fileTypeList[typeNmae]
    const tempFileSuffix = getFileSuffix(name)
    if (typeNmae === '') {
      res.checkResult = true
      res.mesg = ''
    } else {
      if (tempTypes.indexOf(tempFileSuffix) >= 0) {
        res.checkResult = true
        res.mesg = ''
      } else {
        res.mesg = fileTypeErrList[typeNmae]
        res.checkResult = false
      }
    }
  }
  return res
}

export default {
  getFileSuffix,
  getFileGroupName,
  getFielIcon,
  checkFileType
}
